using System;
using UnityEngine;
using Yoozoo.Gta.Common;
using Yoozoo.Managers.ResourceManagement;

namespace MobileMedia
{
    public enum PickMobileCode
    {
        Success = 0,
        MediaPickBusy = 1,  //相册繁忙 ios
        Cancel = 2,  //用户取消
        Permission = 3,  //权限问题
        PathErr = 4,  //路径读取不了对应资源
    }
    public static class MobileMediaHelper
    {


        public static void new_PickMobileImage(int maxSize, Action<Texture2D,int> callBack)
        {
            //异步预请求一次权限
            NativeGallery.RequestPermissionAsync(permission => 
            {
                if (permission != NativeGallery.Permission.Granted)
                {
                    callBack?.Invoke(null,(int)PickMobileCode.Permission);
                    Debug.Log("PickMobileImage Permission result: " + permission);
                }
                else
                {
                    NativeGallery.GetImageFromGallery((path) =>
                    {
                        Debug.Log("Image path: " + path);
                        if (path != null)
                        {
                            // Create Texture from selected image
                            Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize, false, false);
                            if (texture == null)
                            {
                                Debug.Log("Couldn't load texture from " + path);
                                callBack?.Invoke(null,(int)PickMobileCode.PathErr);
                                return;
                            }

                            callBack?.Invoke(texture,(int)PickMobileCode.Success);
                        }
                        else
                        {
                            callBack?.Invoke(null,(int)PickMobileCode.Cancel);
                        }
                    });
                }
            }, NativeGallery.PermissionType.Read, NativeGallery.MediaType.Image);
        }
        /// <summary>
        /// 从文件系统获取图片
        /// </summary>
        /// <param name="maxSize">图片的最大大小</param>
        /// <param name="callBack"></param>
        public static void PickMobileImage(int maxSize, Action<Texture2D,int> callBack)
        {
            if (NativeGallery.IsMediaPickerBusy())
            {
                Debug.LogWarning("UploadImg: Media Picker Busy");
                callBack?.Invoke(null,(int)PickMobileCode.MediaPickBusy);
                return;
            }
            
            //后续更新大版本，可以将new_TakeMobilePicture中的代码移动到这个方法中
            if (AppConfig.BundleVersionCode >= 2510577)
            {
                new_PickMobileImage(maxSize, callBack);
                return;
            }
            //异步预请求一次权限
            NativeGallery.RequestPermissionAsync(permission => 
            {
                    if (permission != NativeGallery.Permission.Granted)
                    {
                        callBack?.Invoke(null,(int)PickMobileCode.Permission);
                        Debug.Log("PickMobileImage Permission result: " + permission);
                    }
                    else
                    {
                        NativeGallery.GetImageFromGalleryNoPermission((path) =>
                            {
                                Debug.Log("Image path: " + path);
                                if (path != null)
                                {
                                    // Create Texture from selected image
                                    Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize, false, false);
                                    if (texture == null)
                                    {
                                        Debug.Log("Couldn't load texture from " + path);
                                        callBack?.Invoke(null,(int)PickMobileCode.PathErr);
                                        return;
                                    }

                                    callBack?.Invoke(texture,(int)PickMobileCode.Success);
                                }
                                else
                                {
                                    callBack?.Invoke(null,(int)PickMobileCode.Cancel);
                                }
                            });
                    }
            }, NativeGallery.PermissionType.Read, NativeGallery.MediaType.Image);
        }
        public static void new_TakeMobilePicture(int maxSize, Action<Texture2D,int> callBack)
        {
            if (NativeCamera.IsCameraBusy())
            {
                callBack?.Invoke(null,(int)PickMobileCode.MediaPickBusy);
                return;
            }

            //异步预请求一次权限
            NativeCamera.RequestPermissionAsync(permission =>
            {
                Debug.Log("NativeCamera.RequestPermissionAsync result: " + permission);
                if (permission != NativeCamera.Permission.Granted)
                {
                    callBack?.Invoke(null,(int)PickMobileCode.Permission);
                }
                else
                {
                    NativeCamera.TakePicture((path) =>
                    {
                        Debug.Log("Image path: " + path);
                        if (!string.IsNullOrEmpty(path))
                        {
                            // Create a Texture2D from the captured image
                            Texture2D texture = NativeCamera.LoadImageAtPath(path, maxSize, false, false);
                            if (texture == null)
                            {
                                Debug.Log("Couldn't load texture from " + path);
                                callBack?.Invoke(null,(int)PickMobileCode.PathErr);
                                return;
                            }

                            callBack?.Invoke(texture,(int)PickMobileCode.Success);
                        }
                        else
                        {
                            callBack?.Invoke(null,(int)PickMobileCode.Cancel);
                        }
                    }, maxSize);
                }
            }, true);
        }
        /// <summary>
        /// 调用手机相机拍照并获取图片
        /// </summary>
        /// <param name="maxSize">图片的最大大小</param>
        /// <param name="callBack"></param>
        public static void TakeMobilePicture(int maxSize, Action<Texture2D,int> callBack)
        {
            if (NativeCamera.IsCameraBusy())
            {
                callBack?.Invoke(null,(int)PickMobileCode.MediaPickBusy);
                return;
            }
            //后续更新大版本，可以将new_TakeMobilePicture中的代码移动到这个方法中
            if (AppConfig.BundleVersionCode >= 2510577)
            {
                new_TakeMobilePicture(maxSize, callBack);
                return;
            }

            //异步预请求一次权限
            NativeCamera.RequestPermissionAsync(permission =>
            {
                Debug.Log("NativeCamera.RequestPermissionAsync result: " + permission);
                if (permission != NativeCamera.Permission.Granted)
                {
                    callBack?.Invoke(null,(int)PickMobileCode.Permission);
                }
                else
                {
                    NativeCamera.TakePictureNoPermission((path) =>
                    {
                        Debug.Log("Image path: " + path);
                        if (!string.IsNullOrEmpty(path))
                        {
                            // Create a Texture2D from the captured image
                            Texture2D texture = NativeCamera.LoadImageAtPath(path, maxSize, false, false);
                            if (texture == null)
                            {
                                Debug.Log("Couldn't load texture from " + path);
                                callBack?.Invoke(null,(int)PickMobileCode.PathErr);
                                return;
                            }

                            callBack?.Invoke(texture,(int)PickMobileCode.Success);
                        }
                        else
                        {
                            callBack?.Invoke(null,(int)PickMobileCode.Cancel);
                        }
                    }, maxSize);
                }
            }, true);
        }


        public static void SendAvatar2Server(string url, Texture2D tex, Action success = null, Action fail = null)
        {
// #if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
//             url += "&platform=mobile";
// #else
//             url += "&platform=pc"; 
// #endif

            UploadImageHandler uploadImageHandler =
                new GameObject("UploadImageHandler").AddComponent<UploadImageHandler>();
            uploadImageHandler.UploadImage2Server(url, tex, () =>
            {
                GameObject.Destroy(uploadImageHandler.gameObject);
                success?.Invoke();
            }, () =>
            {
                GameObject.Destroy(uploadImageHandler.gameObject);
                fail?.Invoke();
            });
        }

        public static void SendChatImage2Server(string url, Texture2D tex, Action success = null, Action fail = null)
        {
// #if (UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR
//             url += "&platform=mobile";
// #else
//             url += "&platform=pc"; 
// #endif

            UploadImageHandler uploadImageHandler =
                new GameObject("UploadImageHandler").AddComponent<UploadImageHandler>();

            uploadImageHandler.UploadImage2Server(url, tex, () =>
            {
                GameObject.Destroy(uploadImageHandler.gameObject);
                success?.Invoke();
            }, () =>
            {
                GameObject.Destroy(uploadImageHandler.gameObject);
                fail?.Invoke();
            });
        }
        
    }
}